<template>
  <div>
    <el-popover
      popper-class="tips"
      placement="bottom-start"
      width="320"
      trigger="click"
      v-model="show"
    >
      <div class="nr">
        <div class="pop_head">
          <div>
            <span
              @click="changeType(1)"
              :class="[popover_type == '1' ? 'active' : '']"
              >调度督办</span
            >
            <span
              @click="changeType(2)"
              :class="[popover_type == '2' ? 'active' : '']"
              >系统通知</span
            >
          </div>
          <el-select v-model="flag" @change="changeFlag">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
        <ul class="pop_main">
          <li v-if="list.length == 0" class="flex-null">
            <img src="~@/assets/img/public/icon-news-none.png" alt="" />
            <p>暂无通知</p>
          </li>
          <li
            v-for="item in list"
            :key="item.flag + '' + item.id"
            @click="changeReadType(item.id, item.flag, item.read_type)"
          >
            <div class="img">
              <img
                v-if="item.read_type !== 0"
                src="~@/assets/img/public/icon-news-yidu.png"
                alt=""
              />
              <img
                v-if="item.read_type == 0"
                src="~@/assets/img/public/icon-news-weidu.png"
                alt=""
              />
              <span class="red-yuan" v-if="item.read_type == 0"></span>
            </div>
            <div class="flex-c" v-if="popover_type == 1">
              <p class="p-title">{{ item.title_name }}</p>
              <p class="p-content">
                {{ item.mes }}
              </p>
              <p class="p-time">{{ item.time }}</p>
            </div>
            <div class="flex-c" v-if="popover_type == 2">
              <p class="p-title">
                <span>{{ item.title_name }}</span
                ><span class="p-time"> {{ item.time }}</span>
              </p>
              <p class="p-content">
                {{ item.mes }}
              </p>
            </div>
          </li>
        </ul>
        <div class="pop_bottom">
          <div @click="readAll" v-if="list.length != 0">
            <img src="~@/assets/img/public/icon-select.png" alt="" />
            <span>标记全部为已读</span>
          </div>
          <div @click="showHistory" class="t-right">
            <img src="~@/assets/img/public/icon-history.png" alt="" />
            <span>历史消息</span>
          </div>
        </div>
      </div>
      <div slot="reference">
        <el-badge :value="weiDu_count" class="item">
          <img src="~@img/header/notice.png" alt="" />
        </el-badge>
      </div>
    </el-popover>

    <el-dialog
      :visible.sync="dialogHistory"
      width="800px"
      :show-close="false"
      :before-close="closeHistory"
    >
      <div class=" dialog_header pop_head">
        <span class="active">历史消息</span>
        <span @click="closeHistory">
          <img src="~@/assets/img/public/icon-close-999.png" alt="" />
        </span>
      </div>
      <ul class="pop_main dialog_main">
        <li v-if="list_history.length == 0">
          <div class="img" style="margin-top: 8px;">
            <img src="~@/assets/img/public/icon-news-none.png" alt="" />
          </div>
          <div class="flex-c">
            暂无历史记录
          </div>
        </li>
        <li
          v-for="item in list_history"
          :key="item.flag + '' + item.id"
          @click="changeReadType(item.id, item.flag, item.read_type)"
        >
          <div class="img">
            <img src="~@/assets/img/public/icon-news-yidu.png" alt="" />
          </div>
          <div class="flex-c">
            <p class="p-title">
              <span>{{ item.title_name }}</span
              ><span class="p-time"> {{ item.time }}</span>
            </p>
            <p class="p-content">
              {{ item.mes }}
            </p>
          </div>
        </li>
      </ul>
      <el-pagination
        @current-change="pageChange"
        :current-page.sync="page"
        :page-size="6"
        layout=" prev, pager, next"
        :total="list_history_total"
      >
      </el-pagination>
    </el-dialog>
  </div>
</template>
<script>
import {
  RemindSearch,
  ReadStatus,
  HistoryMessage,
  WeiDuCount,
} from "@/api/tidings";
export default {
  data() {
    return {
      weiDu_count: "",
      show: false,
      dialogHistory: false,
      popover_type: 1,
      // 1为调度消息，2为系统消息
      // 1为全部，2为未读
      flag: 1,
      options: [
        {
          label: "全部",
          value: 1,
        },
        {
          label: "未读",
          value: 2,
        },
      ],
      //  read_type  0：未读   1：已读   2：已读状态一天后转为历史消息
      list: [],
      list_history: [],
      list_history_total: 0,
      page: 1,
    };
  },
  mounted() {
    this.getWeiDuCount();
    this.getRemindSearch();
  },
  methods: {
    // 获取消息数量
    async getWeiDuCount() {
      let res = await WeiDuCount();
      if (res.code && res.code !== 0) {
        this.weiDu_count = "";
      } else {
        this.weiDu_count = res.data || res == 0 ? "" : res.data || res;
      }
    },
    // 获取消息
    async getRemindSearch() {
      let res = await RemindSearch({
        mes_type: this.popover_type,
        flag: this.flag,
      });
      if (res.code && res.code != 0) {
        this.list = [];
      } else {
        this.list = res;
      }
    },
    // 获取历史消息
    async getHistoryMessage() {
      let res = await HistoryMessage({
        page: this.page,
        per_page: 6,
      });
      if (res.code && res.code !== 0) {
        this.list_history = [];
        list_history_total = 0;
      } else {
        this.list_history = res.mes_list;
        this.list_history_total = res.mes_num;
      }
    },
    // 修改消息状态
    async setReadStatus(read_list) {
      let res = await ReadStatus(read_list);
      if (res.code == 0) {
        this.getRemindSearch();
        this.getWeiDuCount();
      } else {
      }
    },
    showHistory() {
      this.dialogHistory = true;
      this.show = false;
      this.page = 1;
      this.getHistoryMessage();
    },

    // 单个已读   修改  已读  未读
    changeReadType(id, flag, read_type) {
      if (read_type != 0) {
        return;
      }
      let read_list = this.set_read_list([{ flag, id, read_type }]);
      this.setReadStatus(read_list);
    },
    // 全部已读
    readAll() {
      let read_list = this.set_read_list(this.list);
      this.setReadStatus(read_list);
    },
    // 设置将要被 已读的数据
    set_read_list(list) {
      let read_id_list = {
        read_id_list1: [],
        read_id_list2: [],
        read_id_list3: [],
        read_id_list4: [],
        read_id_list5: [],
        read_id_list6: [],
      };
      list.map((item) => {
        if (item.read_type != 0) {
          return;
        }
        switch (item.flag) {
          case 1:
            read_id_list.read_id_list1.push(item.id);
            break;
          case 2:
            read_id_list.read_id_list2.push(item.id);
            break;
          case 3:
            read_id_list.read_id_list3.push(item.id);
            break;
          case 4:
            read_id_list.read_id_list4.push(item.id);
            break;
          case 5:
            read_id_list.read_id_list5.push(item.id);
            break;
          case 6:
            read_id_list.read_id_list6.push(item.id);
            break;
          default:
            return;
        }
      });

      return read_id_list;
    },
    // 设置消息状态 调度督办  系统消息
    changeType(type) {
      this.popover_type = type;
      this.getRemindSearch();
    },
    // 设置消息状态   全部   已读
    changeFlag(flag) {
      this.flag = flag;
      this.getRemindSearch();
      this.getWeiDuCount();
    },
    // 关闭历史弹框
    closeHistory() {
      this.dialogHistory = false;
    },
    // 历史弹框翻页
    pageChange(val) {
      this.page = val;
      this.getHistoryMessage();
    },
  },
};
</script>
<style lang="less" scoped>
.pop_head {
  display: flex;
  justify-content: space-between;
  padding: 0 30px;
  height: 56px;
  line-height: 56px;
  border-bottom: 1px solid #eeeeee;
  span {
    display: inline-block;
    height: calc(100% - 1px);
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #666666;
    cursor: pointer;
    vertical-align: middle;
    &:first-of-type {
      margin-right: 40px;
    }
    &.active {
      font-weight: bold;
      color: #5989db;
      border-bottom: 1px solid #5989db;
    }
  }
}
.pop_main {
  height: 480px;
  overflow: hidden;
  overflow-y: auto;
  scrollbar-color: #ccc #ccc;
  scrollbar-width: 6px;
  scrollbar-width: thin;
  overflow-y: auto;
  &::-webkit-scrollbar {
    width: 10px;
    height: 10px;
  }
  &::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background: linear-gradient(0deg, #ccc, #ccc);
  }
  &::-webkit-scrollbar-track {
    background: rgba(204, 204, 204, 0.1);
    border-radius: 10px;
  }
  li {
    box-sizing: border-box;
    min-height: 120px;
    display: flex;
    padding: 16px 30px 16px 20px;
    border-bottom: 1px solid #eee;
    &.flex-null {
      display: block;
      img {
        width: 33px;
        height: 33px;
        margin-top: 16px;
      }
      text-align: center;
    }
    div.img {
      position: relative;
      flex: 0 0 33px;
      margin-right: 11px;
      cursor: default;
      .red-yuan {
        position: absolute;
        right: -2px;
        top: 0;
        width: 10px;
        height: 10px;
        background: #ff0000;
        border-radius: 50%;
      }
    }
    div.flex-c {
      // display: flex;
      // flex-direction: column;
      // justify-content: space-between;
      p {
        margin-bottom: 14px;
        font-size: 14px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        cursor: default;
        line-height: 18px;
      }
    }
    .p-title {
      color: #333333;
    }
    .p-content {
      color: #999999;
    }
    .p-time {
      color: #cccccc;
    }
  }
}
.pop_bottom {
  height: 60px;
  line-height: 60px;
  padding: 0 30px;
  display: flex;
  justify-content: space-between;
  .t-right {
    flex: 1;
    text-align: right;
  }
  span,
  img {
    cursor: default;
    vertical-align: middle;
  }
  img {
    margin-right: 6px;
  }
}
/deep/ .el-select {
  margin-top: 2px;
  background: none;
  width: 60px;
  padding: 0;
  border: none;
}
/deep/ .el-select .el-input__inner {
  padding: 0;
  border: none;
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #999999;
}
/deep/ .el-icon-arrow-up:before {
  display: inline-block;
  width: 14px;
  height: 8px;
  content: "";
  background: url("~@img/system/down.png") no-repeat center;
  transform: rotate(180deg);
}
/deep/ .el-select__caret {
  height: 30px;
  line-height: 30px;
}

/deep/ .el-dialog__header {
  padding: 0;
}
/deep/ .el-dialog__body {
  padding: 0;
}
/deep/ .el-dialog {
  position: fixed;
  left: 50%;
  top: 50%;
  margin-top: -320px !important;
  margin-left: -400px;
  width: 800px;
  height: 620px;
  background: #ffffff;
  box-shadow: 0px 0px 15px 0px rgba(153, 153, 153, 0.8);
  border-radius: 10px;
  text-align: left;
}
.dialog_header {
  img {
    width: 15px;
    height: 14px;
  }
  span {
    line-height: 56px;
  }
}
.dialog_main {
  li {
    box-sizing: border-box;
    min-height: 80px;
    max-height: 80px;
    div.flex-c {
      flex: 1;
    }
    .p-title {
      display: flex;
      justify-content: space-between;
      color: #333333;
    }
  }
}
/deep/ .el-pagination {
  padding: 20px 30px;
  text-align: right;
}
/deep/ .el-pager li,
/deep/ .el-pagination .btn-next,
/deep/ .el-pagination .btn-prev {
  margin: 4px;
  padding: 0;
  min-width: 30px;
  height: 30px;
  line-height: 30px;
  border-radius: 8px;
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #666666;
  background: #fafafa;
  border: 1px solid #f5f5f5;
  border-radius: 8px;
}
/deep/ .el-pager li.active,
/deep/ .el-pagination .btn-next:hover,
/deep/ .el-pagination .btn-prev:hover {
  background: #5889dc;
  font-weight: bold;
  color: #ffffff;
}
</style>
